//
//  BHMyCompetitionViewController.swift
//  BigHealth
//
//  Created by 蔺百好 on 2021/6/17.
//

import UIKit

class BHMyCompetitionViewController: BHBaseViewController {
    
    private var allTapArray: [CZTopSegMentItemModel] = []

    lazy var pageCycleView: CZPageView = {
        let view = CZPageView.init(frame: CGRect(x: 0, y: UIScreen.begainY, width: UIScreen.width, height: 46))
        view.backgroundColor = .white
        view.delegate = self
        let config = CZPageConfig.init()
        config.showLine = false
        config.nomalFont = .systemFont(ofSize: 14, weight: .medium)
        config.selectFont = .systemFont(ofSize: 14, weight: .bold)
        config.nomalColor = .color_666666
        config.selectColor = .color_333333
        config.itemSpace = 10
        config.leadingSpace = 10
        config.tralingSpace = 5
        
        view.config = config
        return view
    }()
    
    lazy var bigScrollView: UIScrollView = {
        let view = UIScrollView.init(frame: CGRect(x: 0, y: UIScreen.begainY+46, width: UIScreen.width, height: UIScreen.showHeight-46))
        view.isPagingEnabled = true
        view.showsVerticalScrollIndicator = false
        view.showsHorizontalScrollIndicator = false
        view.bounces = false
        view.isScrollEnabled = false
        view.rebuild()
        return view
    }()

    fileprivate var childArray: [BHMyCompetitionChildViewController] = []
    fileprivate var childrenVC: BHMyCompetitionChildViewController!
    
    override func viewDidLoad() {
        super.viewDidLoad()

        self.bh_navigationBar.title_label.text = "热门赛事"
        
        self.view.addSubview(self.pageCycleView)
        self.view.addSubview(self.bigScrollView)
        self.pageCycleView.relationScrollView = self.bigScrollView
        
        self.queryGameStateService()
    }
    

    private func configChild() {
        
        for (index,model) in self.allTapArray.enumerated() {
            let vc = BHMyCompetitionChildViewController.init()
            vc.childType = model.itemId
            self.childArray.append(vc)
            self.bigScrollView.addSubview(vc.view)
            vc.view.frame = CGRect(x: UIScreen.width*CGFloat(index), y: 0, width: UIScreen.width, height: UIScreen.showHeight-46)
            if index == 0 {
                self.childrenVC = vc
            }
        }
        self.bigScrollView.contentSize = CGSize(width: UIScreen.width*CGFloat(self.allTapArray.count), height: 0)
        self.bigScrollView.alwaysBounceVertical = false;
        
        self.childrenVC.refreshChildSouceService()
    }

}

extension BHMyCompetitionViewController: CZPageViewDelegate {
    func pageViewSelectdIndex(_ index: Int, with model: CZTopSegMentItemModel, with pageView: CZPageView) {
        self.childrenVC = self.childArray[index]
        
        self.childrenVC.refreshChildSouceService()
    }
}


extension  BHMyCompetitionViewController {
    func queryGameStateService()  {
        SVProgressHUD.show()
        let manager = BHHTTPClient.init()
        manager.get(url: String.me_myGameState_url, paramters: [:]) { (success, errorMsg, responseDic) in
            SVProgressHUD.dismiss()
            if success == true {
                self.transToModel(dic: responseDic)
            }else {
                BHToast.toast(text: errorMsg)
            }
        }
    }
    
    
    func transToModel(dic: [String:Any])  {
        let result = BHJsonTool.decodeArrayFromDic(dic: dic, key: "result")
        for item in result {
            let name = BHJsonTool.decodeStringFromDic(dic: item as? [String:Any], key: "name")
            let number = BHJsonTool.decodeStringFromDic(dic: item as? [String:Any], key: "num")
            let type = BHJsonTool.decodeStringFromDic(dic: item as? [String:Any], key: "TYPE")

            
            let model = CZTopSegMentItemModel.init()
            model.itemName = "\(name)(\(number))"
            model.itemId = type
            
            self.allTapArray.append(model)
        }
        
        self.pageCycleView.itemArray = self.allTapArray
        self.configChild()

    }
}
